<template>
  <van-nav-bar
    title="订单"
    left-text="返回"
    left-arrow
    @click-left="
      () => {
        $router.push('/');
      }
    "
  />

  <!-- 地址列表 -->
  <van-address-list
    v-model="chosenAddressId"
    :list="list"
    :disabled-list="disabledList"
    default-tag-text="默认"
    @add="onAdd"
    @edit="onEdit"
    :show-add-button="false"
  />

  <div v-for="item in orderData">
    <van-card
      :num="item.number"
      :price="item.goods.goods_price"
      :title="item.goods.goods_name"
      :thumb="item.goods.goods_big_logo"
    >
    </van-card>
  </div>
  <van-submit-bar :price="payData.totalPrice*100" button-text="支付" @submit="onSubmit" />

</template>

<script setup>
import { ref } from "vue";
const list = [
  {
    id: "1",
    name: "张三",
    tel: "13000000000",
    address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
    isDefault: true,
  },
];
//获取详情订单
import { orderDetailApi } from "@/api/api";
//订单id通过购物车传递过来
import { useRoute } from "vue-router";
const route = new useRoute();
//数据不全  获取公共数据
import { useCounterStore } from "@/store/cart";
// 可以在组件中的任意位置访问 `store` 变量 ✨
const store = useCounterStore();

//订单是一组数据
let orderData = ref([]);
orderDetailApi(route.params.orderId).then((res) => {
  console.log("订单详情数据", res);
  orderData.value = res.data.data;
  //获取pinia 中的数据
  let orderAllData = store.carts || [];
  //获取到所有goodsid
  let goodIds = res.data.data.goods.map((item) => +item.goods_id);
  console.log(goodIds);
  //通过拿到的goodsis 筛选数据
  orderAllData = orderAllData.filter((item) => {
    return goodIds.includes(+item.goods.goods_id);
  });
  orderData.value = orderAllData;

  //给支付数据
  payData.value={
    orderId:res.data.data.orderId,
    subject:res.data.data.product_code,
    totalPrice:res.data.data.totalPrice/100
  }
});

//支付
import {payApi} from '@/api/api'
let payData=ref({
  orderId:'',
  subject:'',
  totalPrice:0
})

//点击支付
const onSubmit=()=>{
  payApi(payData.value).then((res)=>{
    window.open(res.data.data,'_blank')
  })
}
</script>

<style lang="scss" scoped></style>
